package com.oocl.crm.repository.impl;

import com.oocl.rcm.db.MyLinkedList;
import com.oocl.rcm.model.Customer;
import com.oocl.rcm.repository.CustomerDataProcess;

public class CustomerDataProcessImpl implements CustomerDataProcess{

	
	
	public void addCustomer(MyLinkedList<Customer> list, Customer c) {
		list.add(c);
	}

	public boolean updateCustomer(MyLinkedList<Customer> list, Customer c) {
		for(int i=0; i<list.getLen(); i++){
			Customer customer = list.get(i).getData();
			if(c.getId() == customer.getId()){
				if((c.getName()!=null)&&!(c.getName().equals(customer.getName()))){
					customer.setName(c.getName());
				}
				if((c.getSex()!=null)&&!(c.getSex().equals(customer.getSex()))){
					customer.setSex(c.getSex());
				}
				if((c.getBirthday()!=null)&&!(c.getBirthday().equals(customer.getBirthday()))){
					customer.setBirthday(c.getBirthday());
				}
				if((c.getCall()!=null)&&!(c.getCall().equals(customer.getCall()))){
					customer.setCall(c.getCall());
				}
				if((c.getAddress()!=null)&&!(c.getAddress().equals(customer.getAddress()))){
					customer.setAddress(c.getAddress());
				}
				return true;
			}
		}
		return false;
	}

	public void deleteCustomer(MyLinkedList<Customer> list, int id) {
		for(int i=0; i<list.getLen(); i++){
			Customer customer = list.get(i).getData();
			if(customer.getId() == id){
				list.remove(i);
			}
		}
	}

	public Customer[] find(MyLinkedList<Customer> list, String condtion) {
		MyLinkedList<Customer> temp = new MyLinkedList<Customer>();
		Customer[] results ;
		
		String[] infos = condtion.split(",");
		int id= -1;
		String name=null,sex=null,birthday=null,call=null,address=null;
		for (int i = 0; i < infos.length; i++) {
			String key = infos[i].split(":")[0];
			String value = infos[i].split(":")[1];
			if("id".equals(key)){
				id = Integer.valueOf(value);
			}
			if("name".equals(key)){
				name = value;
			}
			if("sex".equals(key)){
				sex = value;
			}
			if("birthday".equals(key)){
				birthday = value;
			}
			if("call".equals(key)){
				call = value;
			}
			if("address".equals(key)){
				call = value;
			}
		}
		
		for(int i = 0; i<list.getLen(); i++){
			boolean flag = true;
			if(id != -1 && list.get(i).getData().getId() !=id){
				continue;
			}
			if(name != null && !(list.get(i).getData().getName().equals(name))){
				continue;
			}
			if(sex != null && !(list.get(i).getData().getSex().equals(sex))){
				continue;
			}
			if(call != null && !(list.get(i).getData().getCall().equals(call))){
				continue;
			}
			if(birthday != null && !(list.get(i).getData().getBirthday().equals(birthday))){
				continue;
			}
			if(address != null && !(list.get(i).getData().getAddress().equals(address))){
				continue;
			}
			temp.add(list.get(i).getData());	
		}
		
			
		results = new Customer[temp.getLen()];
		for (int i = 0; i < results.length; i++) {
			results[i] = temp.get(i).getData();
		}
		return results;
	}

	public Customer[] sort(MyLinkedList<Customer> list) {
		Customer[] customers = new Customer[list.getLen()];
		for(int i = 0; i<customers.length; i++){
			customers[i] = list.get(i).getData();
		}
		for(int i = 0; i<customers.length; i++){
			for (int j = 0; j < customers.length-i-1; j++) {
				if((customers[j].compareTo(customers[j+1]))>0){
					Customer temp = customers[j];
					customers[j] = customers[j+1];
					customers[j+1] = temp;
				}
			}
		}
		return customers;
	}
	
	

}
